﻿using Accouting804.Setting;
using Accouting804.ViewModels.AccountViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Accouting804.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewAccountItemPage : ContentPage
    {
        SingleAccoutViewModel viewModel;

        public AccountRecord Item { get; set; }

        public ObservableCollection<PaymentMethod> PaymentMethods { get; set; }
        public ObservableCollection<Category> Categories { get; set; }
        public ObservableCollection<Tag> Tags { get; set; }

        public ObservableCollection<Book> Books { get; set; }

        public NewAccountItemPage()
        {

            InitializeComponent();
            //Item = new AccountRecord()
            //{
            //    Amount = 0,
            //    Catagory = "Default",
            //    DateTime = DateTime.Now,
            //    Description = "描述",
            //    Name = "Name",
            //    Tags = "Default",
            //    User = "test",
            //    Other = "其他说明"
            //};

            var connection = InitializeSetting.AccountConnection();
            var paymentMethods = connection.Table<PaymentMethod>().ToList();



            PaymentMethods = new ObservableCollection<PaymentMethod>(paymentMethods);
            Books = new ObservableCollection<Book>(connection.Table<Book>().ToList());
            Tags = new ObservableCollection<Tag>(connection.Table<Tag>().ToList());


            Categories = new ObservableCollection<Category>(connection.Table<Category>().ToList());


            //book_picker.SelectedIndex = 0;
            //paymentmethods_picker.SelectedIndex = 0;

            //paymentmethods_picker.ItemsSource = PaymentMethods.ToList();
            this.viewModel = new SingleAccoutViewModel()
            {

                Item = new AccountRecord()
                {
                    Amount = 0,
                    Catagory = Categories[0].Name,
                    DateTime = DateTime.Now,
                    Description = "描述",
                    Name = "Name",
                    Tags = "Default",
                    User = "test",
                    Other = "其他说明",
                    PaymentMethod = PaymentMethods[0].Name,
                    Book = Books[0].Name
                },
            };


            BindingContext = this.viewModel;


        }

        private async void cancel_btn_Clicked(object sender, EventArgs e)
        {
            await Navigation.PopModalAsync();
        }

        private async void save_btn_Clicked(object sender, EventArgs e)
        {
            var dtpDate = date_picker.Date;
            var tpTime = time_picker.Time;
            var datetime = new DateTime(dtpDate.Year, dtpDate.Month, dtpDate.Day, tpTime.Hours, tpTime.Minutes, tpTime.Seconds);
            //var currentItem = new AccountRecord()
            //{
            //    Amount = int.Parse(this.amount_entry.Text),
            //    Book = (book_picker.SelectedItem as Book).Name,
            //    Catagory = (categories_picker.SelectedItem as Category).Name,
            //    PaymentMethod = (paymentmethods_picker.SelectedItem as PaymentMethod).Name,
            //    DateTime = datetime,
            //    Description = description_editor.Text,
            //    Name = Item.Name,
            //    User = Item.User
            //};


            var itemFromUI = viewModel.Item;

            //Trace.WriteLine(currentItem.PaymentMethodObj);

            MessagingCenter.Send(this, "AddAccountItem", itemFromUI);
            await Navigation.PopModalAsync();
        }
    }
}